//
//  NewsTableViewCell.m
//  天水在线客户端
//
//  Created by weige on 16/5/4.
//  Copyright © 2016年 weige. All rights reserved.
//

#import "NewsTableViewCell.h"

#import "NewsModel.h"
@interface NewsTableViewCell ()
{
    UILabel *_titleLable;
    UILabel *_subtitleLable;
    UILabel *_timeLable;
    UIImageView *_imgView;
}

@end
@implementation NewsTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self  subLoadView];
        //self.backgroundColor=[UIColor redColor];
    }
    return self;
}

//初始化视图
-(void)subLoadView
{
    _titleLable=[[UILabel alloc]init];
    _titleLable.textColor=TITLECOLOR;
    _titleLable.font=[UIFont systemFontOfSize:TitleFontSize];
    [self.contentView addSubview:_titleLable];
    
    _subtitleLable=[[UILabel  alloc]init];
    _subtitleLable.textColor=SUBTITLECOLOR;
    _subtitleLable.font=[UIFont systemFontOfSize:SubtitleFontsize];
    
    [self.contentView addSubview:_subtitleLable];
    
    _timeLable=[[UILabel alloc]init];
    _timeLable.textColor=SUBTITLECOLOR;
    _timeLable.font=[UIFont systemFontOfSize:SubtitleFontsize];
    [self.contentView addSubview:_timeLable];
    _imgView=[[UIImageView  alloc]init];
    [self.contentView addSubview:_imgView];
    
}

//重写set方法
-(void)setNews:(NewsModel *)news
{
    CGFloat titleX=10,titleY=10;
    //获得title的大小
    CGFloat titleWidth=self.frame.size.width-UITableViewCellControlSpancing*3-UITableViewCellLeftImgWidth;
    CGSize size=[news.title boundingRectWithSize:CGSizeMake(titleWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:TitleFontSize]} context:nil].size;
    _titleLable.text=news.title;
    _titleLable.frame=CGRectMake(titleX, titleY, size.width, size.height);
    
    CGFloat imgX=[[UIScreen mainScreen]bounds].size.width-UITableViewCellControlSpancing-UITableViewCellLeftImgWidth;
    CGFloat imgY=titleY;
    if (news.data) {
        _imgView.image=[UIImage imageWithData:news.data ];
       
    }
    
    else
    {
        _imgView.image=[UIImage imageNamed:@"photo.jpg" ];
    }
    
    _imgView.frame=CGRectMake(imgX, imgY, UITableViewCellLeftImgWidth, UITableViewCellLeftImgHeight);
    
    CGFloat subtitleX=titleX;
    CGFloat subtitleY=CGRectGetMaxY(_imgView.frame);
    CGSize subtitleSize=[news.author sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:SubtitleFontsize]}];
    _subtitleLable.text=news.author;
    _subtitleLable.frame=CGRectMake(subtitleX, subtitleY-subtitleSize.height, subtitleSize.width, subtitleSize.height);
    CGFloat timeX=subtitleX+subtitleSize.width+UITableViewCellControlSpancing*2;
    CGFloat timeY=subtitleY;
    //NSLog(@"%@",news.createDate);
    //NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象
    
    //[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//设定时间格式,这里可以设置成自己需要的格式
    
    NSString *currentDateStr = [NSString stringWithFormat:@"%@",news.createDate];
    NSLog(@"%@",currentDateStr);
    //NSString *date=[NSString stringWit];
    CGSize timeSize=[currentDateStr sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:SubtitleFontsize]}];
    _timeLable.text=currentDateStr;//[NSString stringWithFormat:@"%@",news.createDate];
    _timeLable.frame=CGRectMake(timeX, timeY-timeSize.height, timeSize.width , timeSize.height);
}
@end
